package com.ruoyi.baseData.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.baseData.domain.MaterialType;
import com.ruoyi.baseData.domain.dto.MaterialTypeDto;
import com.ruoyi.baseData.domain.vo.MaterialTypeVo;
import com.ruoyi.baseData.mapper.MaterialTypeMapper;
import com.ruoyi.baseData.service.MaterialTypeService;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.quality.domain.InspectionTemplate;
import com.ruoyi.quality.domain.Sampling;
import com.ruoyi.quality.mapper.InspectionTemplateMapper;
import com.ruoyi.quality.mapper.SamplingMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
*
*/
@Service
public class MaterialTypeServiceImpl implements MaterialTypeService {

    @Resource
    MaterialTypeMapper materialTypeMapper;


    @Resource
    SamplingMapper  samplingMapper;

    @Resource
    InspectionTemplateMapper inspectionTemplateMapper;




    @Override
    public Page<MaterialTypeVo> getMaterialTypeByPage(MaterialTypeDto pageEntity) {
//        new MPJLambdaWrapper<MaterialType>()
//                .selectAll(MaterialType.class)//查询user表全部字段
//                //记的测试阶段
//                .eq(MaterialType::getDelFlag, 0)
//                .orderByDesc(MaterialType::getCreateTime);
        MPJLambdaWrapper<MaterialType> wrapper =  QueryHelp.getPredicate(MaterialType.class, pageEntity);
        wrapper.eq(MaterialType::getDelFlag, 0).orderByDesc(MaterialType::getCreateTime);
        Page<MaterialTypeVo> userList = materialTypeMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   MaterialTypeVo.class, wrapper);
        return userList;
    }

    @Override
    public void addMaterialType(MaterialTypeDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<MaterialType> wrapper = new MPJLambdaWrapper<MaterialType>()
                .eq(MaterialType::getCode, testDto.getCode())
                .ne(MaterialType::getDelFlag, 1);

        MaterialType material = materialTypeMapper.selectOne(wrapper);

        Sampling samplingLab = samplingMapper.selectById(testDto.getSamplingLabId());
        Sampling samplingAppearance = samplingMapper.selectById(testDto.getSamplingAppearanceId());
        Sampling samplingPackage = samplingMapper.selectById(testDto.getSamplingPackageId());
        Sampling samplingSize = samplingMapper.selectById(testDto.getSamplingSizeId());
        Sampling samplingInterface = samplingMapper.selectById(testDto.getSamplingInterfaceId());
        /**
         * 不存在质检模板
         */
//        InspectionTemplate inspectionTemplate = inspectionTemplateMapper.selectById(testDto.getInspectionTemplateId());


        if(null != material) {
            throw new GlobalException("该MaterialType已经存在");
        }


        MaterialType test2 = new MaterialType();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});

//        test2.setInspectionTemplateName(inspectionTemplate.getName());
        test2.setSamplingLabName(samplingLab.getName());
        test2.setSamplingAppearanceName(samplingAppearance.getName());
        test2.setSamplingPackageName(samplingPackage.getName());
        test2.setSamplingSizeName(samplingSize.getName());
        test2.setSamplingInterfaceName(samplingInterface.getName());

        test2.setDelFlag(false);
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        materialTypeMapper.insert(test2);
    }

    @Override
    public void updateMaterialType(MaterialTypeDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<MaterialType> wrapper = new MPJLambdaWrapper<MaterialType>()
                .eq(MaterialType::getId, testDto.getId())
                .eq(MaterialType::getDelFlag, 0);
        MaterialType material = materialTypeMapper.selectOne(wrapper);
        Sampling samplingLab = samplingMapper.selectById(testDto.getSamplingLabId());
        Sampling samplingAppearance = samplingMapper.selectById(testDto.getSamplingAppearanceId());
        Sampling samplingPackage = samplingMapper.selectById(testDto.getSamplingPackageId());
        Sampling samplingSize = samplingMapper.selectById(testDto.getSamplingSizeId());
        Sampling samplingInterface = samplingMapper.selectById(testDto.getSamplingInterfaceId());
//        InspectionTemplate inspectionTemplate = inspectionTemplateMapper.selectById(testDto.getInspectionTemplateId());
        if(null == material) {
            throw new GlobalException("该物料类型不存在");
        }
        BeanUtils.copyProperties(testDto, material);
//        material.setInspectionTemplateName(inspectionTemplate.getName());

        material.setSamplingLabId(samplingLab.getId());
        material.setSamplingLabName(samplingLab.getName());
        material.setSamplingAppearanceId(samplingAppearance.getId());
        material.setSamplingAppearanceName(samplingAppearance.getName());
        material.setSamplingPackageId(samplingPackage.getId());
        material.setSamplingPackageName(samplingPackage.getName());
        material.setSamplingSizeId(samplingSize.getId());
        material.setSamplingSizeName(samplingSize.getName());
        material.setSamplingInterfaceId(samplingInterface.getId());
        material.setSamplingInterfaceName(samplingInterface.getName());
        material.setUpdateBy(userName);
        material.setCreateTime(new Date());
        materialTypeMapper.updateById(material);
    }

    @Override
    public void delMaterialType(MaterialTypeDto testDto) {
        MaterialType material = materialTypeMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        material.setDelFlag(true);
        materialTypeMapper.updateById(material);
    }
}
